package com.模拟线程安全问题.同步方法synchronized;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    private String userID;
    private Double money;


    //取钱的方法
    public synchronized void getMoney(Double money) {//同步方法
//1.先来判断是谁取钱
        String name = Thread.currentThread().getName();
        //2.判断账户是否余额足够
        if (this.money >= money) {
            System.out.println(name + "取走了" + money);
            //更新账户余额
            this.money -= money;
            System.out.println(name + "取钱后剩余" + this.money);
        } else {
            System.out.println(name + "余额不足!");
        }
    }
}
